import 'package:cbec_app/components/base/base_chekbox.dart';
import 'package:cbec_app/components/base/base_function.dart';
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/goods/goods_add_minus.dart';
import 'package:cbec_app/components/goods/goods_info_item.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:left_scroll_actions/left_scroll_actions.dart';

import '../../pages/goods/binding/goods_detail_binding.dart';
import '../../pages/goods/view/goods_detail_page.dart';
import '../../routes/routes.dart';

///购物车商品item
class BagsGoodsItem extends StatefulWidget {
  ///商品分组信息
  final Map<String, dynamic>? groupInfo;

  ///分组索引
  final int? groupIndex;

  ///复选框选中回调
  final CallBackCheckbox? checkboxCallback;

  ///按钮类型
  final RightBtnType btnType;

  ///是否可选规格
  final bool isCanSelect;

  ///删除商品回调
  final Function(String id, int groupIndex, int index)? onDelete;

  ///加入收藏回调
  final Function(String id)? onCollect;

  const BagsGoodsItem({super.key, this.groupInfo, this.btnType = RightBtnType.addMinus, this.groupIndex = 0, this.checkboxCallback, this.onDelete, this.onCollect, this.isCanSelect = false});

  @override
  State<StatefulWidget> createState() => _BagsGoodsItemState();
}

class _BagsGoodsItemState extends State<BagsGoodsItem> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        widget.groupInfo!['activityId'] != null
            ? Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      SizedBox(
                        child: Wrap(
                          runSpacing: 5.w,
                          children: [
                            for (var text in widget.groupInfo?['activityTag'])
                              BorderedText(
                                text: text,
                                padding: const EdgeInsets.only(left: 2, right: 2, top: 2, bottom: 2),
                                borderColor: ThemeConfig.themeColor,
                                textStyle: const TextStyle(fontSize: 11, color: ThemeConfig.themeColor),
                              ),
                          ],
                        ),
                      ),
                      Text(
                        widget.groupInfo!['activityDesc'] ?? '',
                        style: setTextStyle(10, FontWeight.w400, ThemeConfig.mainTextColor),
                      )
                    ],
                  ),
                  goodsTypeTag('优惠券')
                ],
              )
            : emptyWidget(),
        Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start, // 子组件紧密排列
          children: createList(),
        ),
      ],
    );
  }

  ///生成商品列表
  List<Widget> createList() {
    List<Widget> arr = [];
    for (int i = 0; i < widget.groupInfo!['goodsList'].length; i++) {
      var item = widget.groupInfo!['goodsList'][i];
      arr.add(goodsItem(item, i));
    }
    return arr;
  }

  Widget goodsItem(Map<String, dynamic> item, int index) {
    return LeftScroll(
        buttons: [
          ///操作按钮
          BaseInteractive(
            childWidget: Padding(
              padding: const EdgeInsets.symmetric(vertical: 2),
              child: Container(
                width: 44.w,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  color: ThemeConfig.themeColor,
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(10.0), // 右上角圆角
                    bottomRight: Radius.circular(10.0), // 右下角圆角
                  ),
                ),
                child: Text(
                  '删除',
                  style: _textStyle(),
                ),
              ),
            ),
            onClick: () {
              if (widget.onDelete != null) {
                widget.onDelete!(item['cartId'], widget.groupIndex!, index);
              }
            },
          ),
          BaseInteractive(
            childWidget: Padding(
              padding: const EdgeInsets.symmetric(vertical: 2),
              child: Container(
                width: 44.w,
                color: ThemeConfig.addCollectColor,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '收藏',
                      style: _textStyle(),
                    )
                  ],
                ),
              ),
            ),
            onClick: () {
              if (widget.onCollect != null) {
                widget.onCollect!(item['goodsId']);
              }
            },
          ),
        ],
        child: GestureDetector(
          child: GoodsInfoItem(
            infoStyle: InfoStyle.car,
            btnType: widget.btnType,
            goodsInfo: item,
            checkboxCallback: widget.checkboxCallback,
            groupIndex: widget.groupIndex,
            index: index,
            isCanSelect: widget.isCanSelect,
            rightBtnDeleteClick: () {
              if (widget.onDelete != null) {
                widget.onDelete!(item['cartId'], widget.groupIndex!, index);
              }
            },
          ),
          onTap: () {
            final id = widget.groupInfo?['goodsList'][index]['goodsId'];
            Get.to(
              () => GoodsDetailPage(flag: id), // 使用函数懒加载页面实例
              routeName: '${Routes.goodsDetail}?unique=${DateTime.now().millisecondsSinceEpoch}',
              arguments: {"id": id}, // 传递其它参数
              binding: GoodsDetailBinding(flag: id), // 将 tag 参数传递给绑定类
            );
          },
        ));
  }

  ///操作按钮样式
  TextStyle _textStyle() => setTextStyle(12, FontWeight.w500, Colors.white);
}
